package com.doradance.main;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.doradance.R;
import com.doradance.base.BaseFragment;
import com.doradance.main.adapter.CommunityAdapter;
import com.doradance.model.remote.IndexService;
import com.doradance.model.remote.bean.HotListBean;
import com.doradance.net.FailureBean;
import com.doradance.net.HttpHandler;
import com.doradance.net.ServerField;
import com.doradance.net.ServerTaskObserver;
import com.doradance.view.listener.OnLoadMoreListener;
import com.yong.android.framework.view.RefreshLayout;

import butterknife.Bind;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * A simple {@link Fragment} subclass.
 */
public class CommunityFragment extends BaseFragment {


    @Bind(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.mRefreshLayout)
    RefreshLayout mRefreshLayout;
    private CommunityAdapter mAdapter;
    public CommunityFragment() {
        // Required empty public constructor
    }


    @Override
    protected int getFragmentLayout() {
        return R.layout.fragment_community;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);

        mAdapter = new CommunityAdapter(getContext());

        setOnRefreshListener();
        setOnLoadMoreListener();
        mRefreshLayout.notifyRefresh();
        mRecyclerView.setAdapter(mAdapter.getdapter());

        return rootView;
    }

    private void setOnLoadMoreListener() {
        mAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                HttpHandler.create(IndexService.class)
                        .getHotList(mAdapter.getPage(),mAdapter.getPageSize())
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new ServerTaskObserver<HotListBean>() {
                            @Override
                            public void onSuccess(HotListBean value) {
                                mAdapter.updateMore(value.getList());
                            }

                            @Override
                            public void onFailure(FailureBean failureBean) {
                                mAdapter.notifyLoadMoreFailure();
                            }
                            @Override
                            public void onSubscribe(Disposable d) {

                            }
                        });

            }
        });
    }

    private void setOnRefreshListener() {
        mRefreshLayout.setOnRefreshListener(new RefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                HttpHandler.create(IndexService.class)
                        .getHotList(ServerField.SERVER_FIRST_INDEX,mAdapter.getPageSize())
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new ServerTaskObserver<HotListBean>() {
                            @Override
                            public void onSuccess(HotListBean value) {
                                Log.e("AvatarAvatar",value.getList().get(0).getAvatar());
                                mRefreshLayout.notifiyRefreshComplete();
                                mAdapter.updateSource(value == null ? null : value.getList());
                            }

                            @Override
                            public void onFailure(FailureBean failureBean) {
                                mRefreshLayout.notifiyRefreshComplete();
                                mAdapter.notifyFailure();
                                Log.e("AvatarAvatar",failureBean.failureMessage(getContext()));
                            }

                            @Override
                            public void onSubscribe(Disposable d) {

                            }
                        });
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
